package com.valenx.distributedmulticamerasystem.utils;

import com.valenx.distributedmulticamerasystem.camera.constant.RemoteCommand;
import com.valenx.distributedmulticamerasystem.slice.CameraAbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.BufferInfo;
import ohos.rpc.RemoteException;

import java.io.Serializable;
import java.nio.ByteBuffer;

//公共事件工具类
public class CommonEventUtil {
    //日志相关
    static final String TAG = CameraAbilitySlice.class.getSimpleName();
    static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00103, TAG);

    //远程拍摄
    public static void publishRemoteActionEvent() {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.REMOTE_ACTION.action())
                .build();
        intent.setOperation(operation);
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
//-------------------------------------------------------------------------------------------------------------

    //改变远程相机模式
    public static void publishChangeCameraModeEvent(int cameraModeId) {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.CHANGE_CAMERA_MODE.action())
                .build();
        intent.setOperation(operation);
        intent.setParam("CHANGED_CAMERA_MODE_ID",cameraModeId);
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
//-------------------------------------------------------------------------------------------------------------

    //远程切换相机朝向
    public static void publishChangeCameraTypeEvent() {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.CHANGE_CAMERA_TYPE.action())
                .build();
        intent.setOperation(operation);
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
//-------------------------------------------------------------------------------------------------------------

    //从机开始推流
    public static void publishPushFlowEvent() {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.START_PUSHFLOW.action())
                .build();
        intent.setOperation(operation);
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
//-------------------------------------------------------------------------------------------------------------

    //从机停止推流
    public static void publishStopPushFlowEvent() {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.STOP_PUSHFLOW.action())
                .build();
        intent.setOperation(operation);
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
//-------------------------------------------------------------------------------------------------------------

    //从机发送已编码数据给主机
    public static void publishSendEncodedData(byte[] buffers,String fromDeviceId) {
        //创建意图
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(RemoteCommand.SEND_ENCODED_DATA.action())
                .build();
        intent.setOperation(operation);
        intent.setParam("ENCODED_DATA", buffers);//已编码数据
        intent.setParam("FROM_DEVICE_ID",fromDeviceId);//来自设备ID
        //把intent封装到eventData中
        CommonEventData eventData = new CommonEventData(intent);
        //发布eventData
        try {
            CommonEventManager.publishCommonEvent(eventData);
            HiLog.info(LOG_LABEL, "发布远程公共事件成功√。");
        } catch (RemoteException e) {
            HiLog.error(LOG_LABEL, "发布远程公共事件失败。");
        }
    }
}
